package Servlets;

import Beans.PhaseBean;
import Entities.Phase;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name = "PhaseServlet", urlPatterns = {"/PhaseServlet"})
public class PhaseServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            request.getAttribute("id");
            response.setContentType("text/html;charset=UTF-8");

            InputStream inputStream = request.getInputStream();
            ObjectInputStream i = new ObjectInputStream(inputStream);
            String code = (String) i.readObject();

            if ("loadImagePhaseList".equals(code)) {
                Integer worldID = (Integer) i.readObject();
                HttpSession httpSession = request.getSession(true);
                PhaseBean phaseBean = (PhaseBean) httpSession.getAttribute("Bean");
                if (phaseBean == null) {
                    InitialContext context = new InitialContext();
                    phaseBean = (PhaseBean) context.lookup("java:module/PhaseBean");
                    httpSession.setAttribute("Bean", phaseBean);
                }

                ArrayList<String> imagePhaseList = phaseBean.loadImagePhaseList(worldID);

                OutputStream os = response.getOutputStream();
                ObjectOutputStream o = new ObjectOutputStream(os);
                o.writeObject(imagePhaseList);
                o.flush();
                o.close();
            } else if ("loadPhase".equals(code)) {
                Integer worldId = (Integer) i.readObject();
                Integer phaseId = (Integer) i.readObject();

                HttpSession httpSession = request.getSession(true);
                PhaseBean phaseBean = (PhaseBean) httpSession.getAttribute("Bean");
                if (phaseBean == null) {
                    InitialContext context = new InitialContext();
                    phaseBean = (PhaseBean) context.lookup("java:module/PhaseBean");
                    httpSession.setAttribute("Bean", phaseBean);
                }

                Phase phase = phaseBean.loadPhase(worldId, phaseId);
                OutputStream os = response.getOutputStream();
                ObjectOutputStream o = new ObjectOutputStream(os);
                o.writeObject(phase);
                o.flush();
                o.close();
            }
        } catch (Exception ex) {
            try {
                File file = new File("C:/PhaseServlet_processRequest.txt");
                FileOutputStream fos = new FileOutputStream(file);
                ObjectOutputStream o2 = new ObjectOutputStream(fos);
                o2.writeObject(ex.getMessage());
                o2.flush();
                o2.close();
            } catch (Exception e) {
            }
        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
